/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import com.jims.master.common.chart.ChartProcessor;
import com.jims.master.common.chart.ChildTwdChartService;
import com.jims.master.common.chart.TwdChartService;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.ImgToPdfUtil;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.nurse.entity.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.nurse.service.NurseTempInfoService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;


/**
 * 辅助护理项目Controller
 *
 * @author CTQ
 * @version 2017-06-01
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/nurseTempInfo")
public class NurseTempInfoController extends BaseController {

    @Autowired
    private NurseTempInfoService nurseTempInfoService;
    @Autowired
    private PatVisitService patVisitService;

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(NurseTempInfo nurseTempInfo, String type, Model model) {
        if (nurseTempInfo.getInspecttionDate() == null) {
            nurseTempInfo.setInspecttionDate(DateUtils.parseDate(DateUtils.getDate()));
        }
        List<NurseTempInfo> list = nurseTempInfoService.findList(nurseTempInfo);
        if (list != null && list.size() > 0) {
            nurseTempInfo = list.get(0);
        } else {
            nurseTempInfoService.saveTemp(nurseTempInfo);
        }
        model.addAttribute("nurseTempItemInfo", nurseTempInfoService.findItemInfosWithItemDict(nurseTempInfo.getId(), "1".equals(type) ? type : "2"));
        model.addAttribute("nurseTempInfo", nurseTempInfo);
        model.addAttribute("type", type);
        return "modules/nurse/nurseTempIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(NurseTempInfo nurseTempInfo) {
        StringData data = new StringData();
        int i = nurseTempInfoService.saveInfo(nurseTempInfo);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存护理信息成功");
        } else {
            data.setCode("error");
            data.setData("保存护理信息失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "saveItemInfo")
    @ResponseBody
    public StringData saveItemInfo(@RequestBody NurseTempInfo nurseTempInfo) {
        StringData data = new StringData();
        int i = nurseTempInfoService.saveItemInfo(nurseTempInfo);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存护理信息成功");
        } else {
            data.setCode("error");
            data.setData("保存护理信息失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(NurseTempInfo nurseTempInfo) {
        StringData data = new StringData();
        int i = nurseTempInfoService.delete(nurseTempInfo);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除辅助护理项目成功");
        } else {
            data.setCode("error");
            data.setData("删除辅助护理项目失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "generateInfo")
    public String generateInfo(NurseTempInfo nurseTempInfo, String type, HttpServletRequest request, Model model){
        Map<String, Map> map = null;
        if("1".equals(type)) {
            map = nurseTempInfoService.generateChildInfo(nurseTempInfo);
        } else {
            map = nurseTempInfoService.generateInfo(nurseTempInfo);
        }

        String generateDirPath = ImgToPdfUtil.getGenerateDirPath(request, nurseTempInfo.getVisitId(), "img");
        //删除原有图片
        ImgToPdfUtil.deleteFile(new File(generateDirPath));
        //生成图片
        int pageSize = map.keySet().size();
        for(int i = 1; ; i++) {
            Map m = map.get("" + i);
            if(m == null) break;
            ChartProcessor chart = new ChartProcessor();
            m.put("pageInfo", "第 " + i + " 页， 共 " + pageSize + " 页");
            if("1".equals(type)) {
                ChildTwdChartService service = new ChildTwdChartService();
                service.initAllData(chart, m);
            } else {
                TwdChartService service = new TwdChartService();
                service.initAllData(chart, m);
            }

            String imgFilePath = generateDirPath + "page" + i + ".jpg";
            try {
                chart.createChart(null, null, "", "", "", false, 755, 1055, imgFilePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("visitId", nurseTempInfo.getVisitId());
        model.addAttribute("type", type);
        return "modules/nurse/nurseTempLineChart";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "print")
    @ResponseBody
    public StringData print(String pageSize, String page, String visitId, HttpServletRequest request){
        StringData data = new StringData();
        String webPath = null;
        if(StringUtils.isNotBlank(page)) {
            String imgFilePath = ImgToPdfUtil.getGenerateFilePath(request, visitId, "img", "img", "page" + page);
            webPath = ImgToPdfUtil.imgToPdf(imgFilePath, visitId, request);
        } else {
            List<String> imgFilePaths = new ArrayList<>();
            for(int i = 1; i<= Integer.parseInt(pageSize); i++) {
                imgFilePaths.add(ImgToPdfUtil.getGenerateFilePath(request, visitId, "img", "img", "page" + i));
            }
            webPath = ImgToPdfUtil.imgToPdf(imgFilePaths, visitId, request);
        }
        data.setCode("1");
        data.setData(webPath);
        return data;
    }


}